﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReadTxtFile
{
    class Program
    {
        
        static void Main(string[] args)
        {
            int counter = 0;
           
            string line; 
            List<string> mylist = new List<string>();
            List<string> outlist = new List<string>();
           

            System.IO.StreamReader file =
            new System.IO.StreamReader(@"c:\test\afs.txt");
            System.IO.StreamReader file2 =
           new System.IO.StreamReader(@"c:\test\afs.txt");

            System.Console.WriteLine(findEdate(file2).ToString());      
            while (!file.EndOfStream)
            {
                line = file.ReadLine().Trim();
               
                if (!string.IsNullOrEmpty(line))
                {
                    mylist.Add(line.Trim() );
                    
                }
                counter++;
               
            }
          
            mylist.Sort();

            
           
           
            foreach (string s in mylist)
            {
                if (s.Substring(0, 5).ToString() == "TOTAL" || s.Substring(0, 3).ToString().ToUpper() == "###" || s.Substring(0, 3).ToString().ToUpper() == "FOR" || s.Substring(0, 3).ToString().ToUpper() == "JOB" || s.Substring(0, 3).ToString().ToUpper() == "PGM" || s.Substring(0, 3).ToString().ToUpper() == "AGY")
                {
                 
                    mylist.Skip(1);
                   
                }
                else
                {
                    
                    outlist.Add(s);
                  
                }
            }

            string extract = @"c:\test\extract.txt";
            if (File.Exists(extract))
            {
                File.WriteAllLines(extract, outlist);

            }

            System.Console.WriteLine("There were {0} lines.", counter);
           
            System.Console.ReadLine();
        }
        
        private static string findEdate(StreamReader file)
        {
            List<string> removed = new List<string>();
           
             while (!file.EndOfStream)
            {
                string line = file.ReadLine().Trim();
                if (!string.IsNullOrEmpty(line))
                {
                    removed.Add(line.Trim());

                }
                

            }
            
            foreach (string s in removed)
            {
                //FOR THE PERIOD ENDING 06/25/2015
                if (s.Substring(0, 3).ToString().ToUpper() == "FOR")
                {
                    int len = s.Length;
                   
                    return s.Substring(22);

                   
                }

            }

            return string.Empty ;
        }
    }
}
